package com.ds.infra.rpc.autoconfigure;

import com.ds.infra.rpc.autoconfigure.condition.OnRpcProvider;
import com.ds.infra.rpc.autoconfigure.scanner.RpcServiceScanner;
import com.ds.infra.rpc.core.transport.server.NettyServer;
import com.ds.infra.rpc.properties.RpcProperties;
import com.ds.infra.rpc.properties.RpcProviderProperties;
import com.ds.infra.rpc.registry.service.ServiceRegistry;
import com.ds.infra.rpc.registry.service.impl.ZkServiceRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作用：RPC服务提供者自动配置类
 *
 * @author WeiShaoying
 */
@Configuration
@OnRpcProvider
@EnableConfigurationProperties(RpcProviderProperties.class)
@Slf4j
public class RpcProviderAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public RpcServiceScanner rpcServiceScanner() {
        log.info("====> RpcServiceScanner init");
        return new RpcServiceScanner();
    }

    @Bean
    @ConditionalOnMissingBean
    public ServiceRegistry serviceRegistry(RpcProperties properties) {
        log.info("====> ServiceRegistry init");
        return new ZkServiceRegistry(properties.getZkAddress());
    }

    @Bean
    @ConditionalOnMissingBean
    public NettyServer nettyServer(RpcProperties properties) {
        NettyServer server = new NettyServer(properties.getServerPort());
        server.start();
        return server;
    }
}
